import Vue from 'vue'
import App from './App'

import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
// 导入文件
import moment from 'moment'
// 公共JS
import Utils from './common/utils.js'
import ElementUI from 'element-ui'
import vca from '@vue/composition-api'
import i18n from './lang' // internationalization
import store from './store'
import router from './router'
// Flow
import flow from '@flow/constants'

Vue.use(vca)
// 需要汉化
moment.locale('zh-cn')
ElementUI.Dialog.props.closeOnClickModal.default = false
Vue.prototype.$moment = moment
Vue.prototype.$http = axios
const vm = new Vue()
Vue.config.productionTip = false
Vue.prototype.utils = Utils
// AXIOS请求配置
axios.defaults.timeout = 10000
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
// 配置请求的根路径
if (process.env.NODE_ENV === 'development') {
  axios.defaults.baseURL = 'https://dams-portal.yuexiuproperty.cn:8889/market'
} else if (process.env.NODE_ENV === 'dev') {
  axios.defaults.baseURL = 'http://localhost:9005'
} else if (process.env.NODE_ENV === 'production') {
  axios.defaults.baseURL = 'https://dams-portal.yuexiuproperty.cn:8889/market'
} else {
  axios.defaults.baseURL = 'https://dams-portal.yuexiuproperty.cn:8889/market'
}

Vue.prototype.FLOW = flow
Vue.use(ElementUI, {
  i18n: (key, value) => i18n.t(key, value)
})
axios.defaults.baseURL = 'http://localhost:9005'
// axios.defaults.withCredentials = true

axios.interceptors.request.use(config => {
  config.headers.Authorization = window.sessionStorage.getItem('token')
  // config.cookie
  // Nprogress.start()
  // 在最后必须return config
  return config
})

/**
 * 响应拦截
 */
axios.interceptors.response.use(response => {
  if (response.data && response.data.code === 10021) { // 401, token失效
    window.sessionStorage.clear()
    this.$store.commit('delToken')
    this.$store.commit('delActivePathTop')
    router.push({name: 'login'})
  }
  // Nprogress.done()
  return response
}, error => {
  if (error && error.response) {
    switch (error.response.status) {
      case 400:
        console.log('错误请求')
        break
      case 401:
        console.log('未授权,请重新登录')
        break
      case 403:
        console.log('拒绝访问')
        break
      case 404:
        console.log('请求错误,未找到该资源')
        break
      case 405:
        console.log('请求方法未允许')
        break
      case 408:
        console.log('请求超时')
        break
      case 500:
        console.log('服务器端出错')
        break
      case 501:
        console.log('网络未实现')
        break
      case 502:
        console.log('网络错误')
        break
      case 503:
        console.log('服务不可用')
        break
      case 504:
        console.log('网络超时')
        break
      case 505:
        console.log('http版本不支持该请求')
        break
      default:
        console.log(`连接错误${error}`, error.response.data.message)
        vm.$message.error({
          showClose: true,
          offset: 40,
          message:
            `连接错误${error.response.status}: ${error.response.data.message}`
        })
    }
  } else {
    console.log('连接到服务器失败')
  }
  return Promise.resolve(error.response)
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  i18n,
  render: h => h(App)
})
